//
//  BZHomeViewModel.swift
//  weibo
//
//  Created by zzx on 2017/7/30.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit

class BZHomeViewModel: NSObject {

    //数据源
    lazy var statusList: [BZStatusViewModel] = [BZStatusViewModel]()
    
    //加载数据
    func loadData(callBack: @escaping (Bool)->()){
        
        let token = BZUserAccountViewModel.sharedViewModel.accessToken!
        
        BZNetworkTools.sharedTools.requestStatuses(access_token: token) { (response, error) in
            
            if error != nil {
                
                print("error\(error!)")
                
                return
            }
            
            //网络请求成功
            let dic = response as! [String: Any]
            
            //获取微博字典数组
            let statusDicArray = dic["statuses"] as! [[String: Any]]
            
            let statusModelArray = NSArray.yy_modelArray(with: BZStatus.self, json: statusDicArray) as! [BZStatus]
            
            var tempArray = [BZStatusViewModel]()
            
            for statusModel in statusModelArray {
            
                let viewModel = BZStatusViewModel()
                
                viewModel.status = statusModel
                
                tempArray.append(viewModel)
            
            }
            
            
            //赋值
            self.statusList = tempArray
            
            callBack(true)
        }
        
    }
    
}
